package com.dayuanit.service.impl;

import com.dayuanit.controller.request.AddEquipmentParam;
import com.dayuanit.controller.request.EditEquipmentParam;
import com.dayuanit.dao.EquipmentDao;
import com.dayuanit.entity.Equipment;
import com.dayuanit.service.EquipmentService;
import com.dayuanit.util.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired(required = false)
    private EquipmentDao equipmentDao;

    @Override
    public void addEquipment(AddEquipmentParam addEquipmentParam) {
        Equipment equipment = new Equipment();
        BeanUtils.copyProperties(addEquipmentParam,equipment);
        equipment.setLastUpdateTime(new Date());
        equipment.setAddTime(new Date());
        equipment.setIsDelete(0);
        equipmentDao.insert(equipment);
    }



    @Override
    public PageInfo queryEquipmentListByIdOrName(Long id, String name, Integer pageIndex, Integer pageSize) {
        Integer count = equipmentDao.countByIdOrName(id, name);
        PageInfo pageInfo = new PageInfo().setPageNo(pageIndex).setPageSize(pageSize).setTotalCount(count);
        if(count == 0L) {
            return pageInfo.setData(new ArrayList());
        }
        List<Equipment> equipmentList = equipmentDao.queryEquipmentPage(id, name,pageInfo.limitStart(),pageInfo.limitEnd());
        return pageInfo.setData(equipmentList);
    }

    @Override
    public void batchDelete(List<Long> idList) {
        equipmentDao.batchDelete(idList);
    }


    @Override
    public void editEquipment(EditEquipmentParam param) {
        equipmentDao.updateInfoById(param.getId(), param.getEquipmentName(), param.getColor(),
                param.getSize(), param.getPrice(), param.getUseAge(), param.getUseStatus(), param.getHealthStatus());
    }


    @Override
    public List<Equipment> queryEquipment() {
        return equipmentDao.queryEquipment();
    }
}
